<?php
/**
 * PsiFramework
 *
 * Php Simple Framework
 *
 * PHP version 5.3
 *
 * @category  PsiFramework.Plugins.Form.Classes.Services
 * @package   PsiFramework.Plugins.Form.Classes.Services
 * @author    Remi San <remi.san@gmail.com>
 * @copyright 2011 Remi San
 * @license   http://creativecommons.org/licenses/by/3.0/  Creative Commons CC BY 3.0
 * @version   SVN: <svn_id>
 * @link      http://www.psiframework.com/phpdoc/classes/db_\PsiFramework\Plugins\Form\Classes\Services\ListFormsService.html
 */

namespace PsiFramework\Plugins\Form\Classes\Services;

import("Classes.Services.AbstractService");

use \PsiFramework\Classes\Services\AbstractService;
use \PsiFramework\Classes\Factory\DAOFactory;
use \PsiFramework\Classes\Services\Format\JsonFormat;
use \PsiFramework\Classes\Util\HTTP\Request;

/**
 * The ListForms Service
 *
 * This is the service which lists the Forms
 *
 * @category PsiFramework.Plugins.Form.Classes.Services
 * @package  PsiFramework.Plugins.Form.Classes.Services
 * @author   Remi San <remi.san@gmail.com>
 * @license  http://creativecommons.org/licenses/by/3.0/  Creative Commons CC BY 3.0
 * @link     http://www.psiframework.com/phpdoc/classes/db_\PsiFramework\Plugins\Form\Classes\Services\ListFormsService.html
 */
class ListFormsService extends AbstractService
{

    /**
     * Constructor
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * The main function of the method which is called every time
     * this service is created by the front controller.
     *
     * @param Request $request The HTTP request to be treated by the action
     * 
     * @see AbstractService::execute()
     * 
     * @return void 
     */
    public function execute(Request $request)
    {
        parent::execute($request);

        $term = $request->getParameter("term");
        $minisite = $request->getParameter("minisite");
        if (!isNullOrEmptyString($minisite) && strtoupper($minisite) == 'TRUE') {
            $minisite = true;
        } else {
            $minisite = false;
        }

        $formDao = DAOFactory::getDAO("Form");
        $forms = $formDao->retrieveByPartialName($term, false, $minisite);

        $objects = array();
        if (!is_null($forms)) {
            foreach ($forms as $form) {
                $objects[] = new JsonFormat($form->getName(), $form->getId());
            }
        }

        $this->_response->setAttribute("objects", $objects);
        $this->show($this->_format);
    }
}
